package com.demo.mystudy.ui.editfile

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentManager
import com.demo.mystudy.R
import com.demo.mystudy.databinding.DialogEditfileBinding
import com.jetinno.simple.SimpleDialogFragment
import com.jetinno.simple.utils.FragmentsUtil

class EditFileDF : SimpleDialogFragment() {
    lateinit var mDB: DialogEditfileBinding
    override fun useDatabing(inflater: LayoutInflater, container: ViewGroup?): View? {
        mDB = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        return mDB.root
    }

    //这里是绝对地址
    val filePath: String by lazy { requireArguments().getString("data")!! }
    override fun getLayoutId(): Int {
        return R.layout.dialog_editfile
    }

    override fun initView() {
    }

    override fun initEvent() {
        mDB.headbar.setBackClickListener{
            dismiss()
        }
    }

    override fun initData() {
//        lifecycleScope.launch(Dispatchers.IO){
//            val text = FileUtil.fileToString(File(filePath))
//            withContext(Dispatchers.Main){
//                mDB.ceEditfileContent.setText(text)
//            }
//        }
    }

    companion object {
        /**
         * @param filePath 相对路径 例如/Jetinno
         */
        fun showEditFileDF(
            fragmentManager: FragmentManager,
            context: Context,
            filePath: String
        ): EditFileDF {
            val bundle = Bundle()
            bundle.putString("data", filePath)
            return FragmentsUtil.showDialog(
                fragmentManager,
                context,
                EditFileDF::class.java,
                bundle
            )
        }
    }
}